﻿using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
using System.Collections;
using System.Windows.Forms.DataVisualization.Charting;

using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using NPOI.POIFS.Crypt.Dsig;

namespace Module_Debug.VDDIC_Gamma
{
    public partial class A_Demura : Form
    {
        public A_Demura()
        {
            InitializeComponent();
        }
        public static A_Demura aDemura;
        private void A_Demura_Load(object sender, EventArgs e)
        {

        }
        private string Demura_defaultfilePath = "";
        private void btnReload_Demura_File_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            //首次defaultfilePath为空，按FolderBrowserDialog默认设置（即桌面）选择

            if (Demura_defaultfilePath != "")
            {
                //设置此次默认目录为上一次选中目录
                dialog.SelectedPath = Demura_defaultfilePath;
            }
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                //记录选中的目录
                Demura_defaultfilePath = dialog.SelectedPath;
                tBx_Demura_Bin_Path.Text = Demura_defaultfilePath;
            }
            if (!Directory.Exists(tBx_Demura_Bin_Path.Text))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            listBox3_Demura_File.Items.Clear();
            string s = null;
            DirectoryInfo d = new DirectoryInfo(tBx_Demura_Bin_Path.Text);
            FileInfo[] Files = d.GetFiles("*.*");//*.hex|
            // List<string> lstr = new List<string>();

            foreach (FileInfo file in Files)
            {
                s = file.Name.ToLower();
                if (s.EndsWith(".hex") || s.EndsWith(".dsc") || s.EndsWith(".bin") || s.EndsWith(".bmp"))
                {
                    listBox3_Demura_File.Items.Add(s);
                }
            }
        }
        //==================================Demura start==========================================
        private void Demura_OneKey_Operation_Click(object sender, EventArgs e)
        {
            if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37800A_GM862 || Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_GM820)
            {
                //NVT总体烧录过程：Flash使能 -> Flash擦出 -> 进入RAM地址A -> 载入补偿资料A -> 写入Flash内存A
                //驱动芯片复位（大于300ms） -> 进入RAM地址B -> 载入补偿资料B -> 写入Flash内存B
                cbx_write_ram_and_flash_directly.Checked = false;
                Application.DoEvents();
                Demura_Click(Demura_Flash_Enable, null);
                Demura_Click(Flash_Erase_Demura_data, null);
                Demura_Click(Inter_Demura_data, null);
                Load_Demura_data_Click(null, null);
                Demura_Click(Flash_Write_Demura_data, null);
                Thread.Sleep(50);
                //NVT Tool CSV转换并生成两笔Hex文件
                listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
                Application.DoEvents();
                Demura_Click(Inter_Demura_data2, null);
                Load_Demura_data_Click(null, null);
                Demura_Click(Flash_Write_Demura_data2, null);
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_GM040)
            {
                int hex_count = listBox3_Demura_File.Items.Count;
                //NVT总体烧录过程：Flash使能 -> Flash擦出 -> 进入RAM地址A -> 载入补偿资料A -> 写入Flash内存A
                //驱动芯片复位（大于300ms） -> 进入RAM地址B -> 载入补偿资料B -> 写入Flash内存B
                // Flash使能 -> 进入RAM地址C -> 载入补偿资料C -> 写入Flash内存C
                // -> 进入RAM地址D -> 载入补偿资料D -> 写入Flash内存D
                cbx_write_ram_and_flash_directly.Checked = false;
                Demura_Click(Demura_Flash_Enable, null);
                Demura_Click(Flash_Erase_Demura_data, null);
                //加载第一笔
                Application.DoEvents();
                Demura_Click(Inter_Demura_data, null);
                Load_Demura_data_Click(null, null);
                Demura_Click(Flash_Write_Demura_data, null);
                //Thread.Sleep(150);
                //NVT Tool CSV转换并生成两笔Hex文件
                //加载第二笔
                Demura_Click(Demura_Flash_Enable, null); //RE-Start
                listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
                Application.DoEvents();
                Demura_Click(Inter_Demura_data2, null);
                Load_Demura_data_Click(null, null);
                Demura_Click(Flash_Write_Demura_data2, null);

                if (hex_count > 2)
                {
                    //加载第三笔
                    Demura_Click(Demura_Flash_Enable, null); //RE-Start
                    listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data3, null);
                    Load_Demura_data_Click(null, null);
                    Demura_Click(Flash_Write_Demura_data3, null);
                    //加载第四笔
                    Demura_Click(Demura_Flash_Enable, null); //RE-Start
                    listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data4, null);
                    Load_Demura_data_Click(null, null);
                    Demura_Click(Flash_Write_Demura_data4, null);
                }
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37800A_GM040)
            {
                int hex_count = listBox3_Demura_File.Items.Count;
                if (listBox3_Demura_File.SelectedItem.ToString().Contains(".bmp"))   //bmp烧录方式
                {
                    changeHV_37800A_GM040();
                    cbx_write_ram_and_flash_directly.Checked = false;
                    Demura_Click(Demura_Flash_Enable, null);
                    Demura_Click(Flash_Erase_Demura_data, null);
                    //加载第一笔
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data, null);
                    Load_Demura_data_BMP();
                    //   Load_Demura_data_Click(null, null);
                    Demura_Click(Flash_Write_Demura_data, null);
                    Thread.Sleep(150);
                    Demura_Click(Flash_Check_Demura_data, null);
                    //NVT Tool CSV转换并生成两笔Hex文件
                    //加载第二笔
                    Demura_Click(Demura_Flash_Enable, null); //RE-Start
                    listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data2, null);
                    // Load_Demura_data_Click(null, null);
                    Load_Demura_data_BMP();
                    Demura_Click(Flash_Write_Demura_data2, null);
                    Demura_Click(Flash_Check_Demura_data, null);
                }
                else        //bin烧录方式
                {
                    //NVT总体烧录过程：Flash使能 -> Flash擦出 -> 进入RAM地址A -> 载入补偿资料A -> 写入Flash内存A
                    //驱动芯片复位（大于300ms） -> 进入RAM地址B -> 载入补偿资料B -> 写入Flash内存B
                    // Flash使能 -> 进入RAM地址C -> 载入补偿资料C -> 写入Flash内存C
                    // -> 进入RAM地址D -> 载入补偿资料D -> 写入Flash内存D
                    cbx_write_ram_and_flash_directly.Checked = false;
                    Demura_Click(Demura_Flash_Enable, null);
                    Demura_Click(Flash_Erase_Demura_data, null);
                    //加载第一笔
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data, null);

                    Load_Demura_data_Click(null, null);
                    Demura_Click(Flash_Write_Demura_data, null);
                    Thread.Sleep(150);
                    Demura_Click(Flash_Check_Demura_data, null);
                    //NVT Tool CSV转换并生成两笔Hex文件
                    //加载第二笔
                    Demura_Click(Demura_Flash_Enable, null); //RE-Start
                    listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data2, null);
                    Load_Demura_data_Click(null, null);
                    Demura_Click(Flash_Write_Demura_data2, null);
                    Demura_Click(Flash_Check_Demura_data, null);
                }

            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37801_791)                             //NT37801_791
            {
                int hex_count = listBox3_Demura_File.Items.Count;
                if (listBox3_Demura_File.SelectedItem.ToString().Contains(".bmp"))   //bmp烧录方式
                {
                    cbx_write_ram_and_flash_directly.Checked = false;
                    Demura_Click(Demura_Flash_Enable, null);
                    Demura_Click(Flash_Erase_Demura_data, null);
                    //加载第一笔
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data, null);
                    Load_Demura_data_BMP();
                    //   Load_Demura_data_Click(null, null);
                    Demura_Click(Flash_Write_Demura_data, null);
                    Thread.Sleep(150);
                    Demura_Click(Flash_Check_Demura_data, null);
                    //NVT Tool CSV转换并生成两笔Hex文件
                    //加载第二笔
                    Demura_Click(Demura_Flash_Enable, null); //RE-Start     
                    listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data2, null);
                    // Load_Demura_data_Click(null, null);
                    Load_Demura_data_BMP();
                    Demura_Click(Flash_Write_Demura_data2, null);
                    Demura_Click(Flash_Check_Demura_data, null);
                }
                else        //bin烧录方式
                {
                    cbx_write_ram_and_flash_directly.Checked = false;
                    Demura_Click(Demura_Flash_Enable, null);
                    Demura_Click(Flash_Erase_Demura_data, null);
                    //加载第一笔
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data, null);
                    Load_Demura_data_Click(null, null);
                    Demura_Click(Flash_Write_Demura_data, null);
                    Thread.Sleep(200);
                    //        NVT Tool CSV转换并生成两笔Hex文件
                    //        加载第二笔
                    //        Demura_Click(Demura_Flash_Enable, null); //RE-Start     
                    listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
                    Application.DoEvents();
                    Demura_Click(Inter_Demura_data2, null);
                    Load_Demura_data_Click(null, null);
                    Demura_Click(Flash_Write_Demura_data2, null);
                    //       Demura_Click(Flash_Check_Demura_data, null);            
                }

            }
			else if(Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD5306_GM110)
			{
				cbx_write_ram_and_flash_directly.Checked = false;
				Application.DoEvents();
				Demura_Click(Demura_Flash_Enable, null);
 				//Demura_Click(Flash_Erase_Demura_data, null);
 				//Demura_Click(Inter_Demura_data, null);
				Load_Demura_data_Click(null, null);
				Demura_Click(Flash_Write_Demura_data, null);
				Thread.Sleep(50);
				//NVT Tool CSV转换并生成两笔Hex文件
				//listBox3_Demura_File.SelectedIndex = listBox3_Demura_File.SelectedIndex + 1;
				Application.DoEvents();
				//Demura_Click(Inter_Demura_data2, null);
				//Load_Demura_data_Click(null, null);
				//Demura_Click(Flash_Write_Demura_data2, null);
			}
            else
            {
                cbx_write_ram_and_flash_directly.Checked = true;
                Application.DoEvents();
                Demura_Click(Demura_Flash_Enable, null);
                Demura_Click(Flash_Erase_Demura_data, null);
                Demura_Click(Inter_Demura_data, null);
                Load_Demura_data_Click(null, null);
                Demura_Click(Flash_Check_Demura_data, null);
            }
        }
        private byte[] Send_buf = new byte[1];
        private void changeHV_37800A_GM040()
        {
            MessageBox.Show("代码整理，下方内容被屏蔽，具体联系胡凤章", "系统提示：");
            //Send_buf = new byte[25];
            //if (listBox3_Demura_File.SelectedIndex == 0)
            //{
            //    Setting.TempRecord.cmdLen = 25;
            //    UInt32 ICS307Temp = 0x00;
            //    Send_buf[0] = 0x0A; //LCD配置标志
            //    Send_buf[2] = 0x15; //数据长度

            //    if (Mode_Shift_Enable.Checked == false)
            //    {
            //        Send_buf[1] = 0x01;
            //    }
            //    else
            //    {
            //        Send_buf[1] = 0x12;//允许MIPI&SPI在线模式切换
            //    }

            //    Send_buf[3] = (byte)(360 / 256);      //BMP H
            //    Send_buf[4] = (byte)(360 % 256);

            //    Send_buf[5] = (byte)(frmMain.frmain.H_F.Value % 256);
            //    Send_buf[6] = (byte)(H_B.Value % 256);
            //    Send_buf[7] = (byte)(H_S.Value % 256);
            //    Send_buf[8] = (byte)(1201 / 256);     //BMP V
            //    Send_buf[9] = (byte)(1201 % 256);
            //    Send_buf[10] = (byte)(V_F.Value % 256);
            //    Send_buf[11] = (byte)(V_B.Value % 256);
            //    Send_buf[12] = (byte)(V_S.Value % 256);

            //    ICS307Temp = ICS307x.calculate_idt307_config((float)Pixel_Clock.Value);
            //    byte[] byteArray = BitConverter.GetBytes(ICS307Temp);
            //    Send_buf[13] = byteArray[0];
            //    Send_buf[14] = byteArray[1];
            //    Send_buf[15] = byteArray[2];
            //    Send_buf[16] = byteArray[3];
            //    Send_buf[17] = Byte.Parse(txtLCD_CFG.Text.Trim());
            //    Send_buf[18] = (byte)((byte)(Dis_mode.SelectedIndex << 4) + (byte)Sig_mode.SelectedIndex);
            //    Send_buf[19] = signal_mode;

            //    Send_buf[20] = (byte)((Pixel_Clock.Value * 10) % 256);
            //    Send_buf[21] = (byte)((UInt16)(Pixel_Clock.Value * 10) >> 8);
            //    Send_buf[22] = (byte)((byte)((cBx_VCI_Set.SelectedIndex) << 1) + ((byte)(cbx_DVDD_Set.SelectedIndex << 4)) + ((byte)(cBx_VDDI_Set.SelectedIndex << 6)) + (byte)PHY_mode.SelectedIndex);

            //    CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            //    try
            //    {
            //        comPort.Write(Send_buf, 0, 25);
            //    }
            //    catch
            //    {
            //        MessageBox.Show("串口有误，请检查", "错误");
            //        return;
            //    }
            //    Thread.Sleep(100);
            //    SSD28xx_DDIC_InitCodeDownload_Click(btnSsd2828andDDIC, null);

            //}
        }

        private void Load_Demura_data_BMP()
        {
            Int32 H_P_True = 0;
            Int32 H_P_Skip = 0;
            Int32 V_P_True = 0;
            Int32 m_txlen = 0;
            Int32 j = 0;
            FileStream aFile;

            byte[] TemFileContent = new byte[512 * 4000 + 100];
            System.Int32 ByteReaded = 0x00;
            string fn = tBx_Demura_Bin_Path.Text.ToString() + @"\" + listBox3_Demura_File.SelectedItem.ToString();
            aFile = new FileStream(fn, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            ByteReaded = aFile.Read(TemFileContent, 0, 54);
            Int32 BMoffset = ((TemFileContent[13] << 24) + (TemFileContent[12] << 16) + (TemFileContent[11] << 8) + TemFileContent[10]);
            H_P_True = ((TemFileContent[19]) << 8) + TemFileContent[18];
            V_P_True = ((TemFileContent[23] << 8) + TemFileContent[22]);

            if ((frmMain.frmain.Dis_mode.SelectedIndex == 1) && (frmMain.frmain.Pixel_Offset_Enable.Checked == true)) //video
            {
                H_P_Skip = H_P_True % 10 > 0 ? (H_P_True + 10 - H_P_True % 10) : H_P_True;
            }
            else
            {
                H_P_Skip = H_P_True;
            }
            try
            { //5a a5 9DF0731A-----提前下个标志位告诉下位机开始送图了，避免特殊图片格式导致错误配置
                TemFileContent[0] = 0xbd; TemFileContent[1] = 0x5a; TemFileContent[2] = 0xa5; TemFileContent[3] = 0x9d; TemFileContent[4] = 0xf0;
                frmMain.frmain.comPort.Write(TemFileContent, 0, 5);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
            //-----------------------------------
            try
            {
                //H_P_True = Sig_mode.SelectedIndex > 2 ? Convert.ToInt32(2 * ((TemFileContent[19]<<8)+ TemFileContent[18])) : Convert.ToInt32((TemFileContent[19] << 8) + TemFileContent[18]);
                j = 1;
                while (j <= V_P_True)
                {

                    if (H_P_True % 4 != 0)
                        aFile.Seek((V_P_True - j) * (H_P_True * 3 + (H_P_True % 4)) + BMoffset, SeekOrigin.Begin);
                    else
                        aFile.Seek((V_P_True - j) * H_P_True * 3 + BMoffset, SeekOrigin.Begin);

                    ByteReaded = aFile.Read(TemFileContent, 0, H_P_True * 3); // 
                    try
                    {
                        frmMain.frmain.comPort.Write(TemFileContent, 0, H_P_Skip * 3);
                        //  Thread.Sleep(10);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        break;
                    }
                    m_txlen += ByteReaded;
                    if (listBox3_Demura_File.SelectedIndex == 0)
                        progressBar2.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 50);
                    else
                        progressBar2.Value = Convert.ToInt32((double)m_txlen / aFile.Length * 50 + 50);

                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                    j++;
                }

                TemFileContent[0] = 0x5A;
                try
                {
                    frmMain.frmain.comPort.Write(TemFileContent, 0, 1);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    return;
                }
                aFile.Close();

            }
            catch
            {
                frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "啥资料都没有？";
                frmMain.frmain.textBox1.Select(frmMain.frmain.textBox1.Text.Length, 0);
                frmMain.frmain.textBox1.ScrollToCaret();
            }
        }
        CRC16 CRCData = new CRC16();
        public void Demura_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[10];
            Setting.TempRecord.cmdLen = 10;
            Send_buf[0] = 0x2F; //Demura
            Send_buf[2] = 0x05; //Length
            //Send_buf[7] //byte 7区分机种
            if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66455_GM820) //639101
            {
                Send_buf[3] = 0x00;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66456_GM825) //639106
            {
                Send_buf[3] = 0x01;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692A6_GM830) // 583 
            {
                Send_buf[3] = 0x02;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692A9_GM870) // 6088
            {
                Send_buf[3] = 0x03;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69298_GM720) //if 601
            {
                Send_buf[3] = 0x04;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69299_FM720) //if 599
            {
                Send_buf[3] = 0x04;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69298_GM760) //if 621
            {
                Send_buf[3] = 0x05;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69350_GM620) //if 567
            {
                Send_buf[3] = 0x06;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37700C_GM820) //if 639
            {
                Send_buf[3] = 0x07;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_FM010) //if 639
            {
                Send_buf[3] = 0x61;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT5100_GM870) //if 608
            {
                Send_buf[3] = 0x08;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM8A0) //if 635 RM692C9
            {
                Send_buf[3] = 0x09;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692A9_GM8A0) //if 635 RM692A9
            {
                Send_buf[3] = 0x0A;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69200_GM8A0) //if 635 RM69200
            {
                Send_buf[3] = 0x0B;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692A9_GT666) //if 635 RM69200
            {
                Send_buf[3] = 0x0C;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C4_F4) //if 635 RM69200
            {
                Send_buf[3] = 0x0D;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66451_ALPHA) //if 647 R66451_ALPHA
            {
                Send_buf[3] = 0x0E;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66451_GM862) //if 665 -103 
            {
                Send_buf[3] = 0x0F;
                if (ROM1_Tab.Checked == true)
                {
                    //int ttttttt = (Convert.ToString(ROM1_txt.Text));
                    Send_buf[4] = Convert.ToByte((Int32.Parse(ROM1_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 16) & 0xFF);//////////address
                    Send_buf[5] = Convert.ToByte((Int32.Parse(ROM1_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 8) & 0xFF);
                    Send_buf[6] = Convert.ToByte((Int32.Parse(ROM1_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 0) & 0xFF);
                }
                if (ROM2_Tab.Checked == true)
                {
                    Send_buf[4] = Convert.ToByte((Int32.Parse(ROM2_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 16) & 0xFF);//////////address
                    Send_buf[5] = Convert.ToByte((Int32.Parse(ROM2_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 8) & 0xFF);
                    Send_buf[6] = Convert.ToByte((Int32.Parse(ROM2_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 0) & 0xFF);
                }
                if (ROM3_Tab.Checked == true)
                {
                    Send_buf[4] = Convert.ToByte((Int32.Parse(ROM3_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 16) & 0xFF);//////////address
                    Send_buf[5] = Convert.ToByte((Int32.Parse(ROM3_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 8) & 0xFF);
                    Send_buf[6] = Convert.ToByte((Int32.Parse(ROM3_txt.Text, System.Globalization.NumberStyles.HexNumber) >> 0) & 0xFF);
                }
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD5202) || (Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD4202_GM920))
            {
                Send_buf[3] = 0x10; //0x10 please
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM930)    //692udc
            {
                Send_buf[3] = 0x13; //
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69351_GM920)//if 644 RM69351
            {
                Send_buf[3] = 0x14;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM828)//if 639UDC RM692CX
            {
                Send_buf[3] = 0x15;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110C_GM8A0)//if VT6110C_GM8A0(635)
            {
                Send_buf[3] = 0x16;
            }
            //692ax: 6088 Send_buf[3] =  = 0x12;
            //692ax: 641  Send_buf[3] =  = 0x11;
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110_GM750)//if VT6110_GM750(601)
            {
                Send_buf[3] = 0x17;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM010)//3.92
            {
                Send_buf[3] = 0x18;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692D9_GM930)////6.92+2D9
            {
                Send_buf[3] = 0x19;
            }
            ////0x19  for  GM930  692DX
            ///0X1A  for   GM920  692D7
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692D7_GM920)////6.44
            {
                Send_buf[3] = 0x1A;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37800A_GM862)////6.65
            {
                Send_buf[3] = 0x1B;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_GM040)////6.8
            {
                Send_buf[3] = 0x1C;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110C_601)////601
            {
                Send_buf[3] = 0x1D;
            }
            // 0x1F 用于 IL7835 621的
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692EX_DemuraIP_2X4)////2ex   其他位被别人占了，怕怕
            {
                Send_buf[3] = 0x20;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_GM820)////601
            {
                Send_buf[3] = 0x21;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E1_667)////RM692E1_667
            {
                Send_buf[3] = 0x22;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692EX_DemuraIP_4X2)////2ex   RM692EX_DemuraIP_4X2
            {
                Send_buf[3] = 0x23;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692EX_DemuraIP_667)////2ex   RM692EX_DemuraIP_4X2
            {
                Send_buf[3] = 0x24;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692DX_667_GM051))   //667西湖   
            {
                Send_buf[3] = 0x25;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM080))   //643昆仑 
            {
                Send_buf[3] = 0x26;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37800A_GM040))   //NT37800A 680天池 
            {
                Send_buf[3] = 0x27;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM060))   //662华山 
            {
                Send_buf[3] = 0x28;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM070))   //611华山 
            {
                Send_buf[3] = 0x29;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD4202_GM920))   //644梧桐山
            {
                Send_buf[3] = 0x2A;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.VTD6130_XXXXX))  //657
            {
                Send_buf[3] = 0x2B;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.VTD6110C_GM090))  //645
            {
                Send_buf[3] = 0x2C;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.VTD6110C_GM081))  //647
            {
                Send_buf[3] = 0x2D;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM081))  //647
            {
                Send_buf[3] = 0x2E;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD5302_GM056)
            {
                Send_buf[3] = 0x2F;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD5306_GM110)		//6.1
            {
                Send_buf[3] = 0x30;
            }

            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692F0_GM055)		//RM692F0_GM055  6.67 + RM692F0
            {
                Send_buf[3] = 0x31;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E0_GM043)		//6.8DC
            {
                Send_buf[3] = 0x32;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.CH13A11_644)		//6.1
            {
                Send_buf[3] = 0x33;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_D20)		//V3 6.67
            {
                Send_buf[3] = 0x34;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692HX_C16)		//V3 
            {
                Send_buf[3] = 0x36;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692H5_C21)		//V3 
            {
                Send_buf[3] = 0x37;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37801_791)		//V1 
            {
                Send_buf[3] = 0x38;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E5_M344)		//M4 
            {
                Send_buf[3] = 0x39;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6211_670)		//V1 
            {
                Send_buf[3] = 0x3A;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6200_644)		//V1 
            {
                Send_buf[3] = 0x3B;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6300_670_Gamma)		//V1 
            {
                Send_buf[3] = 0x3C;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6300_670_Demura)		//V1 
            {
                Send_buf[3] = 0x3D;
            }
            else
            {
                Send_buf[3] = 0x04;////默认设定为RM69298 + 6.01/6.21模组
            }
            switch (btnTmp.Name)
            {
                case "Demura_Function_ON":
                    {
                        Send_buf[1] = 0x07;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：Demura-ON按下\r\n";
                    }
                    break;
                case "Demura_Function_OFF":
                    {
                        Send_buf[1] = 0x08;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：Demura-OFF按下\r\n";
                    }
                    break;
                case "Inter_Demura_data":
                    {
                        Send_buf[1] = 0x01;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：进入1按下\r\n";
                    }
                    break;
                case "Exit_Demura_data":
                    {
                        Send_buf[1] = 0x02;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：退出按下\r\n";
                    }
                    break;
                case "Flash_Write_Demura_data":
                    {
                        Send_buf[1] = 0x03;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：写入1按下\r\n";
                    }
                    break;
                case "Flash_Check_Demura_data":
                    {
                        Send_buf[1] = 0x04;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：校验按下\r\n";
                    }
                    break;
                case "Flash_Erase_Demura_data":
                    {
                        Send_buf[1] = 0x05;
                        if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66451_GM862) //if 665 -103 
                        {
                            // frmMain.frmain.textBox1.Text = frmMain.frmain.textBox1.Text + "sector erase,please watting 20s+ \r\n";   
                        }
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：擦除按下\r\n";
                    }
                    break;
                case "Demura_Flash_Enable":
                    {
                        Send_buf[1] = 0x06;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：使能按下\r\n";
                    }
                    break;
                case "Flash_Demura_OTP":
                    {
                        Send_buf[1] = 0x09;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：OTP按下\r\n";
                    }
                    break;
                //----------------------------------------------
                case "Flash_Write_Demura_data2":
                    {
                        Send_buf[1] = 0x0A;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：写入2按下\r\n";
                    }
                    break;
                case "Inter_Demura_data2":
                    {
                        Send_buf[1] = 0x0B;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：进入2按下\r\n";
                    }
                    break;
                case "Flash_Write_Demura_data3":
                    {
                        Send_buf[1] = 0x0C;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：写入3按下\r\n";
                    }
                    break;
                case "Inter_Demura_data3":
                    {
                        Send_buf[1] = 0x0D;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：进入3按下\r\n";
                    }
                    break;
                case "Flash_Write_Demura_data4":
                    {
                        Send_buf[1] = 0x0E;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：写入4按下\r\n";
                    }
                    break;
                case "Inter_Demura_data4":
                    {
                        Send_buf[1] = 0x0F;
                        frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：进入4按下\r\n";
                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);
            if (!frmMain.frmain.comPort.IsOpen)
            {
                try
                {
                    frmMain.frmain.comPort.PortName = Detection.GetComNum();
                    frmMain.frmain.comPort.BaudRate = Setting.ComSet.comBaudRate;
                    frmMain.frmain.openSerialPort(true);
                }
                catch
                {
                    MessageBox.Show("没有搜索到串口！", "错误提示");
                }
            }
            try
            {
                frmMain.frmain.comPort.Write(Send_buf, 0, 10);

            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }
        public void Load_Demura_data_Click(object sender, EventArgs e)
        {
            Int32 m_txlen = 0;
            Int32 Once_txlen = 0;
            Int32 File_Length, File_Length_tep;
            bool tx_en = true;
            byte[] TemFileContent = new byte[1024 * 100];
            char[] Acsii_array = new char[2];
            Int32 div = 0;
            //郑州火车站->重新整编包头信息： TemFileContent[0]->IC品牌(Demura补偿数据载入方式不同而区分)；
            //-------------------------------TemFileContent[1]->同颗IC下的衍生项目区分，主要分为：产品分辨率不同或IC对于flash地址排列方式不同; 
            if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66455_GM820)
            {
                div = 256 * 3; //单笔数据切割大小
                TemFileContent[0] = 0x5C;
                TemFileContent[1] = Setting.Demura_Type.R66455_GM820;//0
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66456_GM825)
            {
                div = 256 * 3;
                TemFileContent[0] = 0x5C;
                TemFileContent[1] = Setting.Demura_Type.R66456_GM825;//1
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692A6_GM830)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692A6_GM830; //2
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692A9_GM870)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692A9_GM870; //3
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69298_GM760)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM69298_GM760; //6
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69350_GM620)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM69350_GM620; //7
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66451_ALPHA)
            {
                div = 256 * 3;
                TemFileContent[0] = 0x5C;
                TemFileContent[1] = 0x02;//ALPHA
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.R66451_GM862)
            {
                div = 256 * 3;
                TemFileContent[0] = 0x5C;
                TemFileContent[1] = Setting.Demura_Type.R66451_GM862;//R66451_GM862   占用 0x03-0x05标志位
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37700C_GM820)
            {
                div = 180 * 3;
                TemFileContent[0] = 0x5D;
                TemFileContent[1] = 0x00;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_FM010)
            {
                div = 180 * 3;
                TemFileContent[0] = 0x5D;
                TemFileContent[1] = 0x01;       //NT37701_FM010
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT5100_GM870)
                || (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110C_GM8A0)
                || (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110_GM750))
            {
                div = 180 * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = 0x01;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD5202) || (Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD4202_GM920))
            {
                div = 120;
                TemFileContent[0] = 0x5E;
                TemFileContent[1] = 0x10;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD5306_GM110)
            {
                div = 120;
                TemFileContent[0] = 0x5E;
                TemFileContent[1] = 0x10;
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.CH13A11_644))
            {
                div = 120;
                TemFileContent[0] = 0x5E;
                TemFileContent[1] = 0x10;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69351_GM920)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM69351_GM920; //20
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM828)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692C9_GM828; //20
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM930)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692C9_GM930; //24
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692D7_GM920)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692D7_GM920; //25
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37800A_GM862)
            {
                div = 180 * 3;
                //div = Convert.ToInt16(HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.NT37800A_GM862; //26
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_GM040)
            {
                div = 180 * 3;
                //div = Convert.ToInt16(HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.NT37701_GM040; //27
            }

            //else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692EX_DemuraIP)
            //      {
            //          div = Convert.ToInt16(HACT.Value) * 3;
            //          TemFileContent[0] = 0x5B;
            //          TemFileContent[1] = Setting.Demura_Type.RM692EX_DemuraIP; //28
            //      }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692D9_GM930)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692D9_GM930; //29
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110C_601)
            {
                div = 180 * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.VT6110C_601; //30
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_GM820)
            {
                div = 180 * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.NT37701_GM820; //32
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E1_667)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692E1_667;   //33
                frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "第一笔刷填按下 \r\n";
            }
            else if ((Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692DX_667_GM051) || (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692F0_GM055))
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692DX_667_GM051;   //36
                frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "刷填按下 \r\n";
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM080)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692C9_GM080;   //37
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM081)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692C9_GM081;   //37
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37800A_GM040)
            {
                div = 180 * 3;
                //div = Convert.ToInt16(HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.NT37800A_GM040; //38
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM060)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;//上位机发送bin文件，单次发送量
                //div = Convert.ToInt16(HACT.Value) * 3;
                TemFileContent[0] = 0x5B;  //声明本次发送的buffer是demura烧录文件
                TemFileContent[1] = Setting.Demura_Type.RM692C9_GM060; //40
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692C9_GM070)///6.11+692c9
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;//上位机发送bin文件，单次发送量
                TemFileContent[0] = 0x5B;  //声明本次发送的buffer是demura烧录文件
                TemFileContent[1] = Setting.Demura_Type.RM692C9_GM070; //41
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VTD6130_XXXXX)   //VTD6130 ic验证
            {
                div = 160 * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.VTD6130_XXXXX; //43   2B
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VTD6110C_GM090)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.VTD6110C_GM090; //44   2C
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VTD6110C_GM081)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.VTD6110C_GM090; //45   2D
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.SD5302_GM056)
            {
                div = 120;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.SD5302_GM056; //47   2C
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E0_GM043)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692E0_GM043; // 32
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37701_D20)
            {
                div = 180 * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.NT37701_D20; // 33
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692HX_C16)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692HX_C16; // 33
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692H5_C21)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692H5_C21; // 33
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.NT37801_791)
            {
                div = 180 * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.NT37801_791;                                        //38
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E5_M344)
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.RM692E5_M344;                                  
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6211_670)
            {
                div = 120;//Mutiple of 12Bytes
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.WD6211_670;                                  
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6200_644)
            {
                div = 240;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.WD6200_644;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6300_670_Gamma)
            {
                div = 240;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.WD6300_670_Gamma;
            }
            else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6300_670_Demura)
            {
                div = 240;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = Setting.Demura_Type.WD6300_670_Demura;
            }
            else //瑞鼎ALL D-IC
            {
                div = Convert.ToInt16(frmMain.frmain.HACT.Value) * 3;
                TemFileContent[0] = 0x5B;
                TemFileContent[1] = 0xff;
            }
            System.Int32 ByteReaded = 0x00;

            try
            {
                string FileName = tBx_Demura_Bin_Path.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
                FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                File_Length = Convert.ToInt32(aFile.Length);
				if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6211_670)
				{
					using (FileStream fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write))
					{
						File_Length = File_Length + 256;
						byte[] buffer = new byte[256];
						
						for (int i = 0; i < 256; i++)
						{
							buffer[i] = 0xFF;
						}
						
						fs.Seek(File_Length- 256, SeekOrigin.Begin);
						fs.Write(buffer, 0, 256 - 1);
						
					}
				}
								
                if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E1_667)
                {
                    File_Length = 0x180000;
                }
                //00000000000000000000000000000000000000
                //frmMain.frmain.textBox1.Text += "Send Demura Flag \r\n";
                try
                { //Demura 告诉下位机Demura数据搬运开始
                    TemFileContent[2] = 0xbd;
                    TemFileContent[3] = 0xcf;
                    TemFileContent[4] = 0xfc;
                    TemFileContent[5] = 0xac;
                    TemFileContent[6] = 0xca;
                    TemFileContent[7] = Convert.ToByte(File_Length >> 24);
                    TemFileContent[8] = Convert.ToByte((File_Length >> 16) & 0xff);
                    TemFileContent[9] = Convert.ToByte((File_Length >> 8) & 0xff);
                    TemFileContent[10] = Convert.ToByte(File_Length & 0xff);
                    frmMain.frmain.comPort.Write(TemFileContent, 0, 11);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                    tx_en = false;
                    return;
                }

                if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6300_670_Gamma ||
                    Demura_comboBox.SelectedIndex == Setting.Demura_Type.WD6300_670_Demura)
                { 

                }
                else
                {
                    //00000000000000000000000000000000000000
                    //******add by chenfeng 20190124 if synapic demura data %256 equal to 0, please add 256 byte!
                    if (frmMain.frmain.IC_comboBox.SelectedIndex == Setting.Demura_Type.R66455_GM820 || frmMain.frmain.IC_comboBox.SelectedIndex == Setting.Demura_Type.R66456_GM825)
                    {
                        if (File_Length % 256 == 0)
                        {
                            File_Length = File_Length + 256;
                            //******add by chenfeng 20190124 if synapic demura data %256 equal to 0, please add 256 byte!
                        }
                    }
                }
                
                File_Length_tep = File_Length;

                while (tx_en)
                {
                    if (File_Length_tep > (File_Length % div))
                    {
                        Once_txlen = div;
                    }
                    else  //最后一笔
                    {
                        Once_txlen = Convert.ToInt32(File_Length % div);
                        tx_en = false;
                    }
                    aFile.Seek(0 + m_txlen, SeekOrigin.Begin);    //跳转
                    ByteReaded = aFile.Read(TemFileContent, 0, Once_txlen); // 存入第0位
                    try
                    {
                        if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.VT6110C_601 || Demura_comboBox.SelectedIndex == Setting.Demura_Type.VTD6130_XXXXX) Thread.Sleep(190);
                        frmMain.frmain.comPort.Write(TemFileContent, 0, Once_txlen + 0);
                        //Thread.Sleep(5);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        tx_en = false;
                        break;
                    }
                    //tx_en = false;
                    m_txlen += ByteReaded;
                    progressBar2.Value = Convert.ToInt32((double)m_txlen / File_Length * 100);
                    label_flash.Text = progressBar2.Value.ToString() + "%";
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                    File_Length_tep = File_Length_tep - div;
                }
                frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "第一笔刷填结束 \r\n";
                if (cbx_write_ram_and_flash_directly.Checked == true)
                {
                    Demura_Click(Flash_Write_Demura_data, null);
                }

                //------------------------------载入第二笔数据(瑞鼎)-----------------------
                if ((frmMain.frmain.IC_comboBox.SelectedIndex == Setting.Demura_Type.RM69350_GM620) || (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69351_GM920) || Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E1_667) //瑞鼎（69350/69351）
                {
                    Int32 RM6935x_Demura_part2_addr = 0x180000; //第二笔补偿数据起始地址
                    Int32 RM6935x_Demura_part2_length = 0;//第二笔补偿数据总长，256取整

                    if (frmMain.frmain.IC_comboBox.SelectedIndex == Setting.Demura_Type.RM69350_GM620)
                    {
                        TemFileContent[0] = 0x5B;
                        TemFileContent[1] = Setting.Demura_Type.RM69350_GM620; //7
                        RM6935x_Demura_part2_length = 0x12310F;//第二笔补偿数据总长，256取整
                    }
                    else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM69351_GM920)
                    {
                        TemFileContent[0] = 0x5B;
                        TemFileContent[1] = Setting.Demura_Type.RM69351_GM920; //20
                        RM6935x_Demura_part2_length = 0xEE30B;
                    }
                    else if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E1_667)
                    {
                        TemFileContent[0] = 0x5B;
                        TemFileContent[1] = Setting.Demura_Type.RM692E1_667; //33
                        RM6935x_Demura_part2_length = 0x180000;
                    }
                    frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "第二笔刷填开始 \r\n";
                    File_Length_tep = RM6935x_Demura_part2_length;
                    File_Length = RM6935x_Demura_part2_length;
                    //第一遍发送全部补偿资料，而一笔只发送指定地址以后的资料和长度
                    //00000000000000000000000000000000000000
                    //frmMain.frmain.textBox1.Text += "Send Demura Flag \r\n";
                    try
                    { //Demura 告诉下位机Demura数据搬运开始
                        TemFileContent[2] = 0xbd;
                        TemFileContent[3] = 0xcf;
                        TemFileContent[4] = 0xfc;
                        TemFileContent[5] = 0xac;
                        TemFileContent[6] = 0xca;
                        TemFileContent[7] = Convert.ToByte(File_Length >> 24);
                        TemFileContent[8] = Convert.ToByte((File_Length >> 16) & 0xff);
                        TemFileContent[9] = Convert.ToByte((File_Length >> 8) & 0xff);
                        TemFileContent[10] = Convert.ToByte(File_Length & 0xff);
                        frmMain.frmain.comPort.Write(TemFileContent, 0, 11);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        tx_en = false;
                        return;
                    }
                    //00000000000000000000000000000000000000
                    tx_en = true;
                    m_txlen = 0;
                    while (tx_en)
                    {
                        if (File_Length_tep > (File_Length % div))
                        {
                            Once_txlen = div;
                        }
                        else  //最后一笔
                        {
                            Once_txlen = Convert.ToInt32(File_Length % div);
                            tx_en = false;
                        }
                        aFile.Seek(RM6935x_Demura_part2_addr + m_txlen, SeekOrigin.Begin);    //跳转
                        ByteReaded = aFile.Read(TemFileContent, 0, Once_txlen); // 补偿数据从第0位开始存入
                        try
                        {
                            frmMain.frmain.comPort.Write(TemFileContent, 0, Once_txlen + 0);
                            //Thread.Sleep(5);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            tx_en = false;
                            break;
                        }
                        //tx_en = false;
                        m_txlen += ByteReaded;
                        progressBar2.Value = Convert.ToInt32((double)m_txlen / File_Length * 100);
                        label_flash.Text = progressBar2.Value.ToString() + "%";
                        Application.DoEvents();
                        File_Length_tep = File_Length_tep - div;
                    }
                    frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "第二笔刷填结束 \r\n";
                    if (cbx_write_ram_and_flash_directly.Checked == true)
                    {
                        Demura_Click(Flash_Write_Demura_data2, null);
                    }
                }
                if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E1_667)
                {
                    Int32 RM6935x_Demura_part3_addr = 0x300000; //第二笔补偿数据起始地址
                    Int32 RM6935x_Demura_part3_length = 0;//第二笔补偿数据总长，256取整
                    if (Demura_comboBox.SelectedIndex == Setting.Demura_Type.RM692E1_667)
                    {
                        TemFileContent[0] = 0x5B;
                        TemFileContent[1] = Setting.Demura_Type.RM692E1_667; //7
                        RM6935x_Demura_part3_length = 0xC5006;//第二笔补偿数据总长，256取整
                    }
                    frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "第三笔刷填开始 \r\n";
                    File_Length_tep = RM6935x_Demura_part3_length;
                    File_Length = RM6935x_Demura_part3_length;
                    //第一遍发送全部补偿资料，而一笔只发送指定地址以后的资料和长度
                    //00000000000000000000000000000000000000
                    //frmMain.frmain.textBox1.Text += "Send Demura Flag \r\n";
                    try
                    { //Demura 告诉下位机Demura数据搬运开始
                        TemFileContent[2] = 0xbd;
                        TemFileContent[3] = 0xcf;
                        TemFileContent[4] = 0xfc;
                        TemFileContent[5] = 0xac;
                        TemFileContent[6] = 0xca;
                        TemFileContent[7] = Convert.ToByte(File_Length >> 24);
                        TemFileContent[8] = Convert.ToByte((File_Length >> 16) & 0xff);
                        TemFileContent[9] = Convert.ToByte((File_Length >> 8) & 0xff);
                        TemFileContent[10] = Convert.ToByte(File_Length & 0xff);
                        frmMain.frmain.comPort.Write(TemFileContent, 0, 11);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        tx_en = false;
                        return;
                    }
                    //00000000000000000000000000000000000000
                    tx_en = true;
                    m_txlen = 0;
                    //  temp = 0;
                    while (tx_en)
                    {
                        if (File_Length_tep > (File_Length % div))
                        {
                            Once_txlen = div;
                        }
                        else  //最后一笔
                        {
                            Once_txlen = Convert.ToInt32(File_Length % div);
                            tx_en = false;
                        }
                        aFile.Seek(RM6935x_Demura_part3_addr + m_txlen, SeekOrigin.Begin);    //跳转
                        ByteReaded = aFile.Read(TemFileContent, 0, Once_txlen); // 补偿数据从第0位开始存入
                        try
                        {
                            frmMain.frmain.comPort.Write(TemFileContent, 0, Once_txlen + 0);
                            //Thread.Sleep(5);
                        }
                        catch
                        {
                            MessageBox.Show("串口有误，请检查", "错误");
                            tx_en = false;
                            break;
                        }
                        //tx_en = false;
                        m_txlen += ByteReaded;
                        progressBar2.Value = Convert.ToInt32((double)m_txlen / File_Length * 100);
                        label_flash.Text = progressBar2.Value.ToString() + "%";
                        Application.DoEvents();
                        File_Length_tep = File_Length_tep - div;
                    }
                    frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "第三笔刷填结束 \r\n";
                    if (cbx_write_ram_and_flash_directly.Checked == true)
                    {
                        Demura_Click(Flash_Write_Demura_data3, null);
                        //   while (temp == 0) ;
                    }
                }
                else
                {
                    frmMain.frmain.textBox1.Text += "[" + DateTime.Now.ToLongTimeString().ToString() + "]：" + "载入完成... \r\n";
                }
                aFile.Close();
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }

        }
        private void Demura_Function_ON_MouseEnter(object sender, EventArgs e)
        {
            ToolTip p = new ToolTip();
            p.ShowAlways = true;
            p.SetToolTip(this.Demura_Function_ON, "在此右键，即可DemuraOFF");
        }
        private void Demura_Function_ON_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Demura_Click(Demura_Function_ON, e);
            }
            else
            {
                Demura_Click(Demura_Function_OFF, e);
            }
        }
        //==================================Demura end=============================================
        private void BIN_load_path(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string path = tBx_Demura_Bin_Path.Text;

                if (!Directory.Exists(path))
                {
                    MessageBox.Show("文件夹不存在.");
                    return;
                }
                listBox3_Demura_File.Items.Clear();
                string s = null;
                DirectoryInfo d = new DirectoryInfo(path);
                FileInfo[] Files = d.GetFiles("*.*");//*.hex|
                                                     // List<string> lstr = new List<string>();

                foreach (FileInfo file in Files)
                {
                    s = file.Name.ToLower();
                    if (s.EndsWith(".hex") || s.EndsWith(".dsc") || s.EndsWith(".bin") || s.EndsWith(".bmp"))
                    {
                        listBox3_Demura_File.Items.Add(s);
                    }
                }
                tBx_Demura_Bin_Path.Text = path;
            }
        }
    }
}
